class HtmlUtils {
    static def feedSignatures = [ 'application/rss+xml':'rss',
                                  'application/atom+xml':'atom'
                                 ]
    
    static def extractFeedLink(node) {
        node.breadthFirst().grep {
            it.'@rel'.equals('alternate') && feedSignatures[it.'@type']
        }
    }

    /** convert groovy.util.Node back to xml 
     */
    static def toXml(node) {
        def out = new java.io.StringWriter()
        new XmlNodePrinter(new PrintWriter(out)).print(node)
        return out.toString()
    }

    /** helper method that extract only text value from groovy.util.Node
     *   used like this
     *      buff = []
     *      extractText buff, yourNode
     *      result -> buff.join('')
     */
    static def extractTextHelper(buff, node) {
        if (node instanceof String) {
            buff << node
        } else {
            if (node.children()) {
              node.children().each { extractTextHelper(buff, it) }
            }
        }
    }   

    /** extract text value from groovy.util.Node
     */
    static def extractText(node) {
        def buff = []
        extractTextHelper(buff, node)
        return buff.join(' ')
    }

}
